﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Text;
using BusinessObjects;
using DataObjects.EntityFramework.ModelMapper;

namespace DataObjects.EntityFramework.Implementation
{
    /// <summary>
    /// Entity Framework implementation of the IOrderDao interface.
    /// </summary>
    public class EntityOrderDao : IOrderDao
    {
        /// <summary>
        /// Gets order given an order identifier.
        /// </summary>
        /// <param name="orderId">Order identifier.</param>
        /// <returns>The order.</returns>
        public Order GetOrder(int orderId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                return Mapper.Map(context.OrderEntities.SingleOrDefault(o => o.OrderId == orderId));
            }
        }

        /// <summary>
        /// Gets all orders for a given customer.
        /// </summary>
        /// <param name="customerId">Unique customer identifier.</param>
        /// <returns>List of orders.</returns>
        public List<Order> GetOrders(int customerId, int supplierId, DateTime dateFrom, DateTime dateThru, int status)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                List<OrderEntity>  orders = new List<OrderEntity>();
                if(dateFrom.ToString("mm/dd/yyyy") != "01/01/1990" && dateThru.ToString("mm/dd/yyyy") != "01/01/1990")
                {
                    if (customerId != 0 && supplierId == 0)
                    {
                        orders = context.OrderEntities.Where(o => o.CustomerId == customerId && o.OrderDate >= dateFrom && o.OrderDate <= dateThru && o.Status == status).ToList();
                    }
                    
                    if (customerId == 0 && supplierId != 0)
                    {
                        orders = context.OrderEntities.Where(o => o.SupplierId == supplierId && o.OrderDate >= dateFrom && o.OrderDate <= dateThru && o.Status == status).ToList();
                    }

                    if (customerId != 0 && supplierId != 0) 
                    {
                        orders = context.OrderEntities.Where(o => o.CustomerId == customerId && o.SupplierId == supplierId && o.OrderDate >= dateFrom && o.OrderDate <= dateThru && o.Status == status).ToList();
                    }
                }
                else if (dateFrom.ToString("mm/dd/yyyy") == "01/01/1990" && dateThru.ToString("mm/dd/yyyy") == "01/01/1990")
                {
                    if (customerId != 0 && supplierId == 0)
                    {
                        orders = context.OrderEntities.Where(o => o.CustomerId == customerId && o.Status == status).ToList();
                    }
                    
                    if (customerId == 0 && supplierId != 0)
                    {
                        orders = context.OrderEntities.Where(o => o.SupplierId == supplierId && o.Status == status).ToList();
                    }

                    if (customerId != 0 && supplierId != 0) 
                    {
                        orders = context.OrderEntities.Where(o => o.CustomerId == customerId && o.SupplierId == supplierId && o.Status == status).ToList();
                    }
                }
                    
                var list = new List<Order>();
                foreach (var order in orders)
                    list.Add(Mapper.Map(order));

                return list;
            }
        }

        

        /// <summary>
        /// Gets the orders between a given data range.
        /// </summary>
        /// <param name="dateFrom">Start date.</param>
        /// <param name="dateThru">End date.</param>
        /// <returns></returns>
        public List<Order> GetOrdersByDate(DateTime dateFrom, DateTime dateThru)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var orders = context.OrderEntities.Where(o => o.OrderDate >= dateFrom && o.OrderDate <= dateThru).ToList();

                var list = new List<Order>();
                foreach (var order in orders)
                    list.Add(Mapper.Map(order));

                return list;
            }
        }

        public void InsertOrder(Order order)
        {
            var entity = Mapper.Map(order);

            using (var context = DataObjectFactory.CreateContext())
            {
                context.OrderEntities.AddObject(entity);
                context.SaveChanges();

                // update business object with new version and id
                order.OrderId = entity.OrderId;

                //country.Version = Convert.ToBase64String(entity.Version);

            }
        }
        public void UpdateOrder(Order order)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.OrderEntities.Where(o => o.OrderId == order.OrderId).SingleOrDefault();

                entity.OrderId = order.OrderId;
                entity.CustomerId = order.CustomerId;
                entity.OrderDate = order.OrderDate;
                entity.RequiredDate = order.RequiredDate;
                //ShippedDate = order.ShippedDate,
                entity.Freight = order.Freight;
                entity.Status = order.Status;
                entity.Status = order.Status;

                context.OrderEntities.ApplyCurrentValues(entity);
                context.SaveChanges();

                // Update business object with new version
                order.Version = Convert.ToBase64String(entity.Version);
            }
        }
        public void DeleteOrder(Order order)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.OrderEntities.Where(e => e.OrderId == order.OrderId).SingleOrDefault();
                context.OrderEntities.DeleteObject(entity);
                context.SaveChanges();
            }

        }
    }
}
